/**
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 */

import localStorage from '../utils/localStorage'
import {
  getSign
} from '@/service/upload'
import {
  getSearchDic,
  findByParentCode
} from '@/service/home.js'
import {
  reviewOptionsApi
} from '@/service/govern'

export default {
  // 获取服务设置
  async getFileServerPrefix(ctx) {
    let vuexFix = ctx.state.fileServerPrefix
    let localFix = localStorage.get('fileServerPrefix')
    if (!vuexFix && !localFix) {
      const {
        data
      } = await getSign()
      ctx.commit('SET_SERVER_FIX', data.fileServerPrefix + '/')
      localStorage.set('fileServerPrefix', data.fileServerPrefix + '/')
    } else if (localFix) {
      ctx.commit('SET_SERVER_FIX', localFix)
    }
  },
  // 获取字典数据
  getOption(ctx, code) {
    return new Promise((resolve, reject) => {
      let dict = ctx.state.option
      let localDict = JSON.parse(localStorage.get('dictOption'))

      if (!localDict || dict[code] && dict[code].length == 0 && localDict[code].length == 0) {
        getSearchDic({
          dictionaryCode: code
        }).then(res => {
          ctx.commit('SET_OPTION', {
            key: code,
            value: res.body.list || []
          })
          localStorage.set('dictOption', JSON.stringify(dict))
          resolve(true, res)
        }).catch(err => {
          reject(err)
        })
      } else {
        if (dict[code] && dict[code].length == 0 && localDict[code].length > 0) {
          ctx.commit('SET_OPTION', {
            key: code,
            value: localDict[code]
          })
        }
        resolve(true)
        return false
      }

    })
  },
  // 获取乡镇数据
  getTownOptions(ctx) {
    let option = ctx.state.townOptions
    let townOptions = JSON.parse(localStorage.get('townOptions'))
    if (!townOptions || option && option.length === 0 && townOptions.length === 0) {
      findByParentCode({
          level: 4,
          parentCode: '350923',
        })
        .then((res) => {
          let list = res.body.list || []
          ctx.commit('SET_TOWN', list)
          localStorage.set('townOptions', JSON.stringify(list))
        })
    } else if (option && option.length === 0 && townOptions.length > 0) {
      ctx.commit('SET_TOWN', townOptions)
    }
  },
  // 获取审查项数据
  getReviewOptions(ctx, code) {
    let option = ctx.state.reviewOptions
    // let reviewOptions = JSON.parse(localStorage.get('reviewOptions'))
    // if (!reviewOptions || option && option.length === 0 && reviewOptions.length === 0) {
    if (option && option.length === 0) {
      reviewOptionsApi({
          deptCode: code ? code : '001',
        })
        .then((res) => {
          let list = res.body.list || []
          ctx.commit('SET_REVIEW', list)
          // localStorage.set('reviewOptions', JSON.stringify(list))
        })
      // } else if (option && option.length === 0 && reviewOptions.length > 0) {
      //   ctx.commit('SET_REVIEW', reviewOptions)
    }
  },
}